package se.karolinska.corticostriatal.crosshair;

import ij.Prefs;
import ij.gui.MessageDialog;
import java.awt.Color;
import java.util.LinkedList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.WindowConstants;
import org.micromanager.utils.ReportingUtils;

/**
 *  Show a crosshair settings dialog. 
 * 
 *  This enables the user to set preferences like crosshair size.
 * 
 *  @author Matthijs
 */
public class CrosshairDialog extends javax.swing.JFrame {

    /** Creates new form CrosshairDialog */
    public CrosshairDialog() {
        initComponents();
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        this.setTitle("Crosshair settings");        
        sizeSlider.setValue((int) Prefs.get("micromanager.crosshair.sizeSlider", 50));
        this.addColorOptions();
    }

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        toggleCrosshair = new javax.swing.JToggleButton();
        sizeSlider = new javax.swing.JSlider();
        sizeLabel = new javax.swing.JLabel();
        colorLabel = new javax.swing.JLabel();
        colorBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        toggleCrosshair.setText("Show Crosshair");
        toggleCrosshair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggleCrosshairActionPerformed(evt);
            }
        });

        sizeSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sizeSliderStateChanged(evt);
            }
        });

        sizeLabel.setFont(new java.awt.Font("Dialog", 0, 14));
        sizeLabel.setText("Relative size");

        colorLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        colorLabel.setText("Color");

        colorBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colorBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(toggleCrosshair, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sizeLabel)
                            .addComponent(colorLabel))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sizeSlider, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                            .addComponent(colorBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(toggleCrosshair, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sizeLabel)
                    .addComponent(sizeSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(colorLabel)
                    .addComponent(colorBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void toggleCrosshairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toggleCrosshairActionPerformed
        ReportingUtils.logMessage("Toggling crosshair display.");
        
        if (toggleCrosshair.isSelected()) {
            try {
                CrosshairOverlay.getInstance().show();
                toggleCrosshair.setText("Hide crosshair");            
            } catch (Exception e) {
                ReportingUtils.logError("Could not enable crosshair.");
                new MessageDialog(this, "Error showing crosshair", "The crosshair could not be shown. Is the live image window active?"); 
                toggleCrosshair.setSelected(false);
            }
        }
        else {
            CrosshairOverlay.getInstance().hide();
            toggleCrosshair.setText("Show crosshair");
        }
    }//GEN-LAST:event_toggleCrosshairActionPerformed
    
    private void sizeSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sizeSliderStateChanged
        Prefs.set("micromanager.crosshair.sizeSlider", sizeSlider.getValue());
        CrosshairOverlay.getInstance().setSize(sizeSlider.getValue());
    }//GEN-LAST:event_sizeSliderStateChanged

    private void colorBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colorBoxActionPerformed
        Color item = (Color) ((ColorOption) colorBox.getSelectedItem()).getColor();
        Prefs.set("micromanager.crosshair.colorBox", colorBox.getSelectedIndex());
        CrosshairOverlay.getInstance().setColor(item);        
    }//GEN-LAST:event_colorBoxActionPerformed


    /**
     *  Add options to the color-selection dropdown. These are ColorOptions,
     *  which combine a description with a Color object.
     */
    private void addColorOptions () {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        model.addElement(new ColorOption(Color.WHITE,   "White"));
        model.addElement(new ColorOption(Color.RED,     "Red"));
        model.addElement(new ColorOption(Color.ORANGE,  "Orange"));
        model.addElement(new ColorOption(Color.BLUE,    "Blue"));        
        model.addElement(new ColorOption(Color.GREEN,   "Green"));
        colorBox.setModel(model);
        colorBox.setSelectedIndex((int) Prefs.get("micromanager.crosshair.colorBox", 1));
    }

    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CrosshairDialog().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox colorBox;
    private javax.swing.JLabel colorLabel;
    private javax.swing.JLabel sizeLabel;
    private javax.swing.JSlider sizeSlider;
    private javax.swing.JToggleButton toggleCrosshair;
    // End of variables declaration//GEN-END:variables
}
